var DatePickers = function () {

    var handleDatePickers = function () {
        if (jQuery().datepicker) {
            $('.form_date').datetimepicker({
            	language:  'zh-CN',
                format: 'yyyy-mm-dd',
                weekStart: 1,
                todayBtn: 1,
                todayHighlight: 1,
                startView: 2,
        		minView: 2,
                orientation: "left",
                forceParse: 0,
                autoclose: true
            });
            
            $('.form_date_cn').datetimepicker({
            	language:  'zh-CN',
                format: 'yyyy年mm月dd日',
                weekStart: 1,
                todayBtn: 1,
                todayHighlight: 1,
                startView: 2,
        		minView: 2,
                orientation: "left",
                forceParse: 0,
                autoclose: true
            });
            //$('body').removeClass("modal-open"); // fix bug when inline picker is used in modal
            
            $('.form_date_mon').datetimepicker({
            	language:  'zh-CN',
                format: 'yyyy-mm',
                weekStart: 1,
                todayBtn: 1,
                todayHighlight: 1,
                startView:3,
        		minView: 3,
        		maxView: 4,
                orientation: "left",
                forceParse: 0,
                autoclose: true
            });
        }

    }

    var handleTimePickers = function () {

        if (jQuery().timepicker) {
            $('.form_time12').timepicker({
                autoclose: true,
                showSeconds: true,
                minuteStep: 1,
                secondStep: 1
            });
            
            $('.form_time12-no-seconds').timepicker({
                autoclose: true,
                minuteStep: 1
            });
            
            $('.form_time24').timepicker({
                autoclose: true,
                showSeconds: true,
                showMeridian: false,
                minuteStep: 1,
                secondStep: 1
            });

            $('.form_time24-no-seconds').timepicker({
                autoclose: true,
                minuteStep: 1,
                showSeconds: false,
                showMeridian: false
            });

            // handle input group button click
            $('.timepicker').parent('.input-group').on('click', '.input-group-btn', function(e){
                e.preventDefault();
                $(this).parent('.input-group').find('.timepicker').timepicker('showWidget');
            });
        }
    }

    var handleDateRangePickers = function () {
        if (!jQuery().daterangepicker) {
            return;
        }
        
        $('.form_daterange').daterangepicker({
                opens: 'left',
                format: 'MM/DD/YYYY',
                separator: ' to ',
                startDate: moment().subtract(29,'days'),
                endDate: moment(),
                minDate: '01/01/2005',
                maxDate: '12/31/2025'
            },
            function (start, end) {
                $('.form_daterange input').val(start.format('YYYY/MM/DD') + ' - ' + end.format('YYYY/MM/DD'));
            }
        );
        
        $('.form_daterange_cn').daterangepicker({
            opens: 'left',
            format: 'MM/DD/YYYY',
            separator: ' to ',
            startDate: moment().subtract(29,'days'),
            endDate: moment(),
            minDate: '01/01/2005',
            maxDate: '12/31/2025'
        },
        function (start, end) {
            $('.form_daterange input').val(start.format('YYYY年MM月DD日') + ' - ' + end.format('YYYY年MM月DD日'));
        }
    );
        
    }

    var handleDatetimePicker = function () {

        if (!jQuery().datetimepicker) {
            return;
        }

        $(".form_datetime").datetimepicker({
            autoclose: true,
            language:'zh-CN',
            weekStart: 1,
            todayBtn: 1,
            todayHighlight: 1,
            format: "yyyy-mm-dd hh:ii",
            forceParse: 0,
            pickerPosition: "bottom-right"
        });
        
        $(".form_hourtime").datetimepicker({
            autoclose: true,
            language:'zh-CN',
            startView:'hour',
            minuteStep:10,
            format: "hh:ii",
            forceParse: 0,
            pickerPosition: "bottom-right"
        });
        
        $(".form_datetime_cn").datetimepicker({
            autoclose: true,
            language:'zh-CN',
            weekStart: 1,
            todayBtn: 1,
            todayHighlight: 1,
            format: "yyyy年mm月dd日 hh时ii分",
            forceParse: 0,
            pickerPosition: "bottom-right"
        });
        
        $(".form_datetime_no_second").datetimepicker({
            autoclose: true,
            language:'zh-CN',
            weekStart: 1,
            todayBtn: 1,
            todayHighlight: 1,
            format: "yyyy-mm-dd hh:00",
            minView: 1,
    		maxView: 2,
            forceParse: 0,
            pickerPosition: "bottom-right"
        });

        $('body').removeClass("modal-open"); // fix bug when inline picker is used in modal
    }

    return {
        //main function to initiate the module
        init: function () {
            handleDatePickers();
            handleTimePickers();
            handleDatetimePicker();
            handleDateRangePickers();
        }
    };

}();